﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace dmm
{
    class Options
    {
        private string outdir =@"c:\c2v_demo\";

        private void create_folders()
        {
            if (!Directory.Exists(outdir))
            {
                Directory.CreateDirectory(outdir);
            }
            if (!Directory.Exists(VideoOutPath))
            {
                Directory.CreateDirectory(VideoOutPath);
            }
            if (!Directory.Exists(ThumbnailOutPath))
            {
                Directory.CreateDirectory(ThumbnailOutPath);
            }
            if (!Directory.Exists(EdlOutPath))
            {
                Directory.CreateDirectory(EdlOutPath);
            }
            if (!Directory.Exists(WorkPath))
            {
                Directory.CreateDirectory(WorkPath);
            }
        }
        public String OutputPath
        {
            get
            {
                //if(outdir == null) outdir = Properties.Settings.Default.workdir;
                if (outdir == null || outdir == "")
                {
                    outdir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Sothink Cnvas to Video\";
                    create_folders();
                }
                return outdir;
            }
            set 
            { 
                if(Directory.Exists(value))
                {
                    outdir = value;
                    create_folders();
                    //Properties.Settings.Default.workdir = value;
                   // Properties.Settings.Default.Save();
                }
            }
        }

        public string VideoOutPath 
        {
            get{
               return outdir+@"video\";
            }
        }

        public string ThumbnailOutPath
        {
            get
            {
               return outdir+@"thumbnail\";
            }
        }


        public string EdlOutPath
        {
            get
            {
                return outdir + @"edl\";
            }
        }

        public string DBFile
        {
            get { return outdir + @"video.db"; }
        }


        public string WorkPath
        {
            get {return outdir+@"tmp\";}
        }
       
        //public string player=@"g:\tv\mplayer.exe";
       // public string encoder = @"G:\media_tools\old\mencoder\MPlayer-rtm-svn-34401\mencoder.exe";
        //public string player = @"G:\media_tools\old\mencoder\MPlayer-rtm-svn-34401\mplayer.exe";
        //public string player = @"D:\Program Files (x86)\SMPlayer\mplayer\mplayer.exe";
      
        private static Options instance;

        public string AssemplyPath
        {
            get { return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); }
        }

        public String PlayerPath
        {
            get{ return ( AssemplyPath+@"\mplayer\mplayer.exe");}
        }

        public String EncoderPath
        {
            get { return (AssemplyPath + @"\mplayer\mencoder.exe"); }
        }

        private Options()
        {
            
           

            if (!Directory.Exists(ThumbnailOutPath))
            {
                Directory.CreateDirectory(ThumbnailOutPath);
            }

            if (!Directory.Exists(EdlOutPath))
            {
                Directory.CreateDirectory(EdlOutPath);
            }

            
        }

        public static Options Instance
        {
            get
            {
                if (instance == null)
                    instance = new Options();
                return instance;
            }   
        }

        public static string newUniqueFile(string path,string name, string ext)
        {
            Trace.Write("enter newUniqueFile\n");
            string filename = path+@"\"+name+"."+ext;
            int idx = 0;
            while (true)
            {
                
                if(!System.IO.File.Exists(filename))
                {
                    try{
                        System.IO.FileStream s = System.IO.File.Create(filename);
                        s.Close();
                        break;
                    }catch(Exception){
                    }
                }
                idx++;
                filename = String.Format(@"{0}\{1}_{2}.{3}",path, name, idx, ext);

            }

            Trace.Write("leave newUniqueFile\n");
            return filename;
        }
    }
}
